<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/18 0018
 * Time: 下午 3:12
 */

namespace app\index\controller;

use app\admin\model\Product;
use app\admin\model\Indent;
use app\admin\model\User as UserModel;
use Alidayu\Alidayu;
use Kd\Kuaidi;
use think\Db;
use think\Request;
use think\Session;

class User extends Common
{
    /**
     * @title   个人中心
     * @return mixed
     */
    public function index()
    {
        $user = new UserModel();
        $res = $user::get(function($query){
            $query ->where('id',Session::get('userid'))->field('u_head,u_nickname,u_username');
        })->toarray();
        $this->assign('res',$res);
        return $this->fetch('index/center');
    }

    /**
     * @title   绑定银行卡
     * @return mixed
     */
    public function cardBind()
    {
        if(Request::instance()->isAjax()){
            //保存银行卡信息
            $data = ['id'=>Session::get('userid'),'bank_name'=>input('post.bn'),'bank_username'=>input('post.bu'),'bank_number'=>input('post.bsn')];
            $ret = $this->store((new UserModel()),$data);
            if($ret) {
                $phone = (new UserModel())->where('id', Session::get('userid'))->value('u_username');
                $this->code($phone);
                $this->success('发送验证码成功');
            }
        }else {
            $bank_msg = (new UserModel())->field('bank_name,bank_number,bank_username')->find(Session::get('userid'));
            $this->assign('bank',$bank_msg);
            return $this->fetch('index/card_bind');
        }
    }

    /**
     * @title   发送短信验证码
     */
    public function code($phone)
    {
        $rand = rand(100000, 999999);
        Session::set('bank_code', $rand);
        $alidayu = new Alidayu();
        $content = '{"phone":"' . $rand . '"}';
        $result = $alidayu->sendSms($phone, 'SMS_56610214', $content);
        //发送成功后记录到文件中
        if ($result == 0) {
            $status = '验证码发送成功【绑定银行卡成功后开通账号通知】';
            rwLog($phone, $status);
        } else {
            $status = '验证码发送失败【绑定银行卡成功后开通账号通知】';
            rwLog($phone, $status);
        }
    }

    /**
     * @title   验证短信验证码
     */
    public function validateCode()
    {
        if(input('post.code')==Session::get('bank_code')) $this->success('绑定成功','index');
        else $this->error('验证码错误');
    }

    /**
     * @title   订单物流
     * @param id
     * @return mixed
     */
    public function logistics($id=null)
    {
        $pid = (new Indent())->where('id',$id)->value('i_pid');
        $p_photo = (new Product())->where('id',$pid)->value('p_photo');
        $this->assign('photo',$p_photo);

        $find = Db::name('logistics')->where('indent_id', $id)->find();
        $kd = new Kuaidi();
        $res = json_decode($kd->getTransport($find['express_name'],$find['express_number']),true);
        $this->assign('res',$res);

        dump($res);die;

        $express_type = config('express_type');
        $this->assign('express_type',$express_type[$res['state']]);
        return $this->fetch('index/logistics');
    }
}